package com.onemile.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onemile.app.base.BaseUI;
import com.onemile.app.base.BaseUINoBottom;
import com.onemile.controls.AdpLV_Cart;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.utils.UtiDialogsFactory;

public class UICart extends BaseUINoBottom
{
   private TextView txtCartEmpty;
   private LinearLayout layCart;
   private ListView listCart;
   private ImageView imgGoSettle;
   private ImageView imgContinue;

   private void InitControls()
   {
	  txtCartEmpty = (TextView) findViewById(R.id.txtCartEmpty_cart);
	  layCart = (LinearLayout) findViewById(R.id.layCart_cart);
	  listCart = (ListView) findViewById(R.id.listCart_cart);
	  imgGoSettle = (ImageView) findViewById(R.id.imgGoSettle_cart);
	  imgGoSettle.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			if (Session.getInstance().HasDifferentShopsCommodity())
			{
               UtiDialogsFactory.getInstance().ShowToastInfoShort(UICart.this, getString(R.string.cart_has_different_shop));
			}
			else
			{
			   if (Session.getInstance().IsLogin())
			   {
				  Intent intent = new Intent();
				  intent.setClass(UICart.this, UISettlement.class);
				  startActivity(intent);
			   }
			   else
			   {
				  Intent intent = new Intent();
				  intent.setClass(UICart.this, UILogin.class);
				  intent.putExtra(PubConst.Intent_Key.LOGIN_FROM_ACTIVITY, PubConst.ActivityCode.CART);
				  startActivityForResult(intent, PubConst.ActivityCode.CART);
			   }
			}
		 }
	  });
	  imgContinue = (ImageView) findViewById(R.id.imgContinue_cart);
	  imgContinue.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			finish();
		 }
	  });
   }

   private void InitListView()
   {
	  AdpLV_Cart adp = new AdpLV_Cart(Session.getInstance().getCommodityList(), UICart.this);
	  listCart.setAdapter(adp);
   }

   @Override
   protected void onCreateOverride(Bundle savedInstanceState)
   {
	  InitControls();
	  InitListView();
   }

   public void SetDisplay()
   {
	  try
	  {
		 int goodTypeNum = Session.getInstance().GetCommodityCount();
		 if (goodTypeNum > 0)
		 {
			layCart.setVisibility(View.VISIBLE);
			txtCartEmpty.setVisibility(View.GONE);
		 }
		 else
		 {
			layCart.setVisibility(View.GONE);
			txtCartEmpty.setVisibility(View.VISIBLE);
		 }
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "BaseUI_RefreshCartNum() ", e);
		 }
	  }
   }

   @Override
   protected void onResume()
   {
	  super.onResume();
	  SetDisplay();
   }

   @Override
   public int SetContentViewLayoutResId()
   {
	  return R.layout.cart;
   }

   @Override
   public void SetTopToolbar(TextView topTxt, ImageView leftImBtn, ImageView rightImgBtn, RelativeLayout rightRay, TextView cartNum)
   {
	  topTxt.setText(getString(R.string.title_cart));
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if(keyCode==event.KEYCODE_BACK)
	  {
		finish(); 
	  }
      return false;
   }
   
   

}
